//
//  LYRadioHomeController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 梁媛. All rights reserved.
//

#import "LYRadioHomeController.h"
#import "LYRadioHomeCell.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "LYModel.h"
#import "LYsmallModel.h"
#import "UIImageView+WebCache.h"
#import "LYheadView.h"
#import "LunboModel.h"
#import "SDCycleScrollView.h"
#import "UIView+Extention.h"
#import "LYListController.h"
#import "UIButton+WebCache.h"
#import "MJRefresh.h"
@interface LYRadioHomeController ()<SDCycleScrollViewDelegate>
@property(nonatomic,strong)YPMakeURL *makeURL;
@property(nonatomic,strong)YPNetWorking *netWorking;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)LYheadView *LYHV;
@property(nonatomic,strong)NSMutableArray *dataArray2;
@property(nonatomic,strong)NSMutableArray *dataArray3;
@end

@implementation LYRadioHomeController
static  NSInteger flag = 0;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"LYRadioHomeCell" bundle:nil] forCellReuseIdentifier:@"LYRadioHomeCell"];
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self handleData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreData)];
}

- (void)refreshAction{
    [self handleData];
}

- (void)handleData{
    flag = 0;
    self.makeURL = [YPMakeURL shareMakeURL];
    self.netWorking = [YPNetWorking shareNetWorking];
    [self.netWorking getDictWithURL:self.makeURL.radioURL dict:self.makeURL.radioWheelDic success:^(NSDictionary *dic) {
        for (NSDictionary *dictionary in dic[@"data"][@"alllist"]) {
            LYModel *model = [[LYModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            model.userinfo = [[LYsmallModel alloc]init];
            [model.userinfo  setValuesForKeysWithDictionary:dictionary[@"userinfo"]];
            [self.dataArray addObject:model];
        }
        [self.dataArray2 removeAllObjects];
        for (NSDictionary *dictio in dic[@"data"][@"hotlist"]) {
            LYModel *model = [[LYModel alloc] init];
            [model setValuesForKeysWithDictionary:dictio];
            [self.dataArray2 addObject:model];
        }
        [self.dataArray3 removeAllObjects];
        for (NSDictionary *dictionar in dic[@"data"][@"carousel"]) {
            LunboModel *modelll = [[LunboModel alloc] init];
            [modelll setValuesForKeysWithDictionary:dictionar];
            [self.dataArray3 addObject:modelll];
        }
        NSMutableArray *whell = @[].mutableCopy;
        for (LunboModel *model in self.dataArray3) {
            [whell addObject:model.img];
        }
        self.tableView.tableHeaderView = [self setupView:whell buttonArr:self.dataArray2];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError *err) {
    }];
}

- (void)upLoadMoreData{
        flag += 9;
        [self.netWorking getDictWithURL:self.makeURL.radioListURL dict:[self.makeURL makeRadioAllListDicWithRadioId:flag] success:^(NSDictionary *dic) {
            for (NSDictionary *dictionary in dic[@"data"][@"list"]) {
                LYModel *model = [[LYModel alloc] init];
                [model setValuesForKeysWithDictionary:dictionary];
                model.userinfo = [[LYsmallModel alloc]init];
                [model.userinfo  setValuesForKeysWithDictionary:dictionary[@"userinfo"]];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        } fail:^(NSError *err) {
            return ;
        }];
}

- (UIView*)setupView:(NSArray*)arr  buttonArr:(NSArray<LYModel*>*)buttons{
    CGFloat btnW = (self.view.width - (buttons.count + 1)*5) / buttons.count;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.width,btnW + 190)];
    SDCycleScrollView *scroll = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,view.width,150) imageURLStringsGroup:arr];
    scroll.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    scroll.delegate = self;
    [view addSubview:scroll];
    for (int i = 0; i < buttons.count; i++) {
        LYModel *model = buttons[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tag = i + 200;
        button.frame = CGRectMake(5 + (btnW + 5) * i,CGRectGetMaxY(scroll.frame) + 5,btnW,btnW);
        [button sd_setBackgroundImageWithURL:[NSURL URLWithString:model.coverimg] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(hotListAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, btnW + 163,110,20)];
    lable.font = [UIFont systemFontOfSize:10];
    lable.text = @"全部电台.All stations";
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = [UIColor grayColor];
    [view addSubview:lable];
    UIView *littleView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lable.frame),btnW + 173, self.view.width - lable.width - 10,1)];
    littleView.backgroundColor = [UIColor grayColor];
    [view addSubview:littleView];
   return view;
}
- (void)hotListAction:(UIButton*)sender{
    LYListController *LYC = [[LYListController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:LYC];
    NSInteger count = sender.tag - 200;
    LYModel *model = self.dataArray2[count];
    LYC.passId = model.radioid;
    [self.navigationController presentViewController:navi animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYRadioHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LYRadioHomeCell" forIndexPath:indexPath];
    LYModel *model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    cell.titleLabel.text = model.title;
    cell.uNameLabel.text = [NSString stringWithFormat:@"by: %@",model.userinfo.uname];
    cell.descLabel.text = model.desc;
    cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)model.count];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LYListController *LYC = [[LYListController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:LYC];
    LYModel *model = self.dataArray[indexPath.row];
    LYC.passId = model.radioid;
    [self.navigationController presentViewController:navi animated:YES completion:nil];
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)dataArray2{
    if (!_dataArray2) {
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;
}
- (NSMutableArray *)dataArray3{
    if (!_dataArray3) {
        _dataArray3 = [NSMutableArray array];
    }
    return _dataArray3;
}
@end
